﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

using Input;
using Microsoft.Xna.Framework.Content;

namespace GUILibrary
{
    public class GUIScreen
    {
        private List<Button> buttons;
        private List<Backdrop> backdrops;
        private List<Component> components;

        public List<Button> Buttons
        {
            get { return buttons; }
            set { buttons = value; }
        }

        public List<Backdrop> Backdrops
        {
            get { return backdrops; }
            set { backdrops = value; }
        }

        public List<Component> Components
        {
            get { return components; }
            set { components = value; }
        }

        /// <summary>
        /// The GUIScreenManager who is managing this GUIScreen.
        /// </summary>
        [ContentSerializerIgnore]
        public GUIScreenManager Manager
        {
            get;
            set;
        }

        public GUIScreen()
        {
            Buttons = new List<Button>();
            Backdrops = new List<Backdrop>();
        }

        /// <summary>
        /// Goes through the list of buttons to check for hover.  If
        /// the mouse is within the bounds of the button, it sets
        /// the button to hover.
        /// </summary>
        /// <param name="MousePosition"></param>
        public void UpdateHover(Vector2 MousePosition)
        {
            foreach (Button button in buttons)
            {
                Rectangle rect = button.Bounds;
                //rect.X = button.Position.X;
                //rect.Y = button.Position.Y;
                if(rect.Contains((int)MousePosition.X, (int)MousePosition.Y))
                {
                    button.Hovered = true;
                }
                else
                {
                    button.Hovered = false;
                }
            }

            foreach (Component c in components)
            {
                c.UpdateHover(MousePosition.X, MousePosition.Y);
            }
        }

        public void UpdateMouseClick(GameInput input)
        {
            foreach (Component c in components)
            {
                c.UpdateMouseClick(input);
            }
        }

        /// <summary>
        /// Returns the Component with the given name.  If there doesn't
        /// exist such a component, return null.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Component GetComponentByName(string name)
        {
            foreach (Component c in components)
            {
                if (c.Name.Equals(name))
                    return c;
            }

            return null;
        }

        public string GetAction()
        {
            foreach (Button button in buttons)
            {
                if (button.Hovered)
                    return button.Action;
            }
            
            return null;
        }

        public List<string> GetComponentActions()
        {
            List<string> actions = new List<string>();
            foreach (Component c in components)
            {
                if (c.Triggered)
                {
                    if(c.Action != null)
                        actions.Add(c.Action);
                }
            }

            return actions;
        }

        public void Draw(SpriteBatch batch)
        {
            foreach (Backdrop back in backdrops)
            {
                back.Draw(batch);
            }

            foreach (Button button in buttons)
            {
                button.Draw(batch);
            }

            foreach (Component c in components)
            {
                c.Draw(batch);
            }
        }

        /// <summary>
        /// Loop through the components and set their GUIScreen.
        /// </summary>
        public void RegisterComponents(ContentManager cm)
        {
            foreach (Component c in components)
            {
                c.ParentScreen = this;
                c.Load(cm);
            }
        }

        /// <summary>
        /// Register the GUIScreen to the GUIScreenManager.  Also adds
        /// all Components that are ActionListeners to the GUIScreenManager.
        /// </summary>
        /// <param name="manager"></param>
        public void RegisterToManager(GUIScreenManager manager)
        {
            Manager = manager;

            ActionListener listener = null;
            foreach (Component c in components)
            {
                listener = c as ActionListener;
                if (listener != null)
                    manager.AddActionListener(listener);
            }
        }
    }
}
